<?php
namespace app\adminjcb\controller;

use app\common\controller\AdminBaseController;

/**
 * 商品管理
 * Class Member
 */
class GoodsController extends AdminBaseController {
	public function glist(){
		$map = array();
		$goods_list = db('goods')->where($map)->paginate(15, false, get_query());
		$this->assign('list', $goods_list);
		return $this->fetch();
	}
	/**
	 * 列表
	 * @param string $keyword
	 * @param int    $page
	 * @return mixed
	 */
	public function index() {
		if ($this->request->isPost()) {
			$id = input('id');
			$data['name'] = input('name');
			$data['en_name'] = input('en_name');
			$data['price'] = input('price');
			$data['stock'] = input('stock');
			$data['add_time'] = input('add_time');
			$data['end_time'] = input('end_time');
			$data['status'] = input('status');
			if (input('thumb_img')) {
				$data['thumb'] = input('thumb_img');
			}
			if (db('goods')->where('id', $id)->update($data) !== false) {
				$this->success('更新成功',"glist");
			} else {
				$this->error('更新失败',"glist");
			}
		} else {
			$goods = db('goods')->find(input("id"));
			$this->assign('model', $goods);
			return $this->fetch();
		}
	}
	/**
	 * 列表
	 * @param string $keyword
	 * @param int    $page
	 * @return mixed
	 */
	public function add() {
		if ($this->request->isPost()) {
			$data['name'] = input('name');
			$data['en_name'] = input('en_name');
			$data['price'] = input('price');
			$data['stock'] = input('stock');
			$data['add_time'] = input('add_time');
			$data['end_time'] = input('end_time');
			$data['status'] = input('status');
			if (input('thumb_img')) {
				$data['thumb'] = input('thumb_img');
			}
			if (db('goods')->insert($data) !== false) {
				$this->success('添加成功',"glist");
			} else {
				$this->error('添加失败',"glist");
			}
		} else {
			return $this->fetch();
		}
	}
}
